<template>
  <div>
    <div>
      <div>
        <el-table
            max-height="500"
            highlight-current-row
            ref="multipleTable"
            tooltip-effect="dark"
            border
            :data="tableData"
            style="margin: 0 auto; width: 100%;"
        >
          <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
          <el-table-column prop="name" label="姓名" header-align="center" align="center"></el-table-column>

          <el-table-column prop="password" label="密码" header-align="center" align="center"></el-table-column>

          <el-table-column prop="role" label="角色" header-align="center" align="center"></el-table-column>
          <el-table-column prop="agencyId" label="归属机构" header-align="center" align="center">
            <template slot-scope="scope">
              {{ getAgencyName(scope.row.agencyId) }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="账号状态" header-align="center" align="center"></el-table-column>
          <el-table-column label="操作" width="400" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="primary"
                  plain
                  @click="updateInfo(scope.row.id)"
              >编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog :visible.sync="dialogVisible" :before-close="close">
        <el-form :model="user" size="mini">
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="user.name" placeholder="请输入姓名" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="user.password" placeholder="请输入密码" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
              label="手机号"
              :label-width="formLabelWidth"
              prop="phone"
              :rules="[
            { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
            ]">
            <el-input v-model="user.phone" placeholder="请输入手机号" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
              label="身份证号"
              :label-width="formLabelWidth"
              prop="idNumber"
              :rules="[
            { pattern: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/, message: '身份证号格式不正确', trigger: 'blur' }
            ]">
            <el-input v-model="user.idNumber" placeholder="请输入身份证号" autocomplete="off"></el-input>
          </el-form-item>

        </el-form>


        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false;clear()">取 消</el-button>
          <el-button  type="primary" @click="addForm()">确 定</el-button>
        </div>
      </el-dialog>


    </div>

  </div>


</template>
<script>


import {addUser, changeUser, deleteUser, getAllUsers, getInfo} from "@/api/user";
import {getAgencyList} from "@/api/agency";

export default {
  data() {
    return {
      background: true,
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      // 添加数据对话框是否展示的标记
      dialogVisible: false,
      // 表格数据
      tableData: [],
      addOrUpdate: true,//true为add
      user: {"password": "1234567"},
      form: {},
      posts: [],
      multipleSelection: [],
      selectedIds: [],
      agencyList: [],
      formLabelWidth: "80px",
      theMajorOfMy: "",
    }
  },
  methods: {
    validateAge(rule, value, callback) {
      if (value === undefined || value === null || value === '') {
        return callback(new Error('请输入年龄'));
      }
      if (value < 0 || value > 100) {
        callback(new Error('年龄必须在0到100之间'));
      } else {
        callback();
      }
    },

    show() {
      getInfo().then((res) => {
        let p=[]
        if (res.data.code === 1) {
          p.push(res.data.data)
          this.tableData = p;
          console.log(this.tableData)
        }
      }).catch((e) => {
      })
    },
    clear() {
      this.user = {};
      this.show();
    },
    add() {
      this.user = {};
      this.user.password = "1234567";
      this.addOrUpdate = true;
      this.dialogVisible = true;
    },
    addForm() {

      changeUser(this.user).then(res => {
        console.log(res)
        if (res.data.code === 1) {
          this.$message.success("更新成功")
          this.dialogVisible = false;
          this.user.status = "正常";
          this.show();
        }
      }).catch(e => {
      });


    },

    close() {
      this.dialogVisible = false;
      this.user = {}
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    updateInfo(id) {
      this.addOrUpdate = false;
      this.user = this.tableData.filter(data => data.id == id)[0];
      this.dialogVisible = true;
    },
    getAllAgencyList() {
      getAgencyList().then((res) => {
        if (res.data.code === 1) {
          this.agencyList = res.data.data;
        }
      }).catch((e) => {
      })
    },
    getAgencyName(id) {
      let agency = this.agencyList.find(data => data.id === id);
      return agency ? agency.name : "";
    },


  },
  mounted() {
    this.show();
    this.getAllAgencyList();
  },
};
</script>


<style>
* {
  margin: 0;
  padding: 0;
}

.el-header {
  background-color: rgb(238, 241, 246);
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: white;
}

</style>
<script setup>
</script>
